import {
    Message
} from 'element-ui'
import axios from 'axios';

var ajax = axios.create({
    baseURL: process.env.BASE_URL, //测试
    headers: {
        APPID: 1,
    },
    withCredentials: true, //cookie
    crossDomain: true //跨域
});

//添加一个请求拦截器
ajax.interceptors.request.use(function(config) {
    //在请求发出之前进行一些操作
    var user = localStorage.getItem('user') || '{}';
    var obj = JSON.parse(user);
    if(obj.token){
        config.headers['TOKEN'] = obj.token
    }
    return config;
}, function(err) {
    console.error(err);
    Message.error({
        message: '请求发生错误,请稍后再试!',
        type: 'error'
    });
    //Do something with request error
    return Promise.reject(err);
});
//添加一个响应拦截器
ajax.interceptors.response.use(function(res) {
    var status = res.data.status;
    if(!status){
        Message.error({
            message: res.data.message,
            type: 'error'
        });
        return Promise.reject(res.data.message)
    }
    return (res.data.data ? res.data.data : res.data.status);
}, function(err) {
    console.error(err);
    Message.error({
        message: '响应发生错误,请稍后再试!',
        type: 'error'
    });
    //Do something with response error
    return Promise.reject(err);
})

export const getPeriodVideo = params => {
    return ajax.get('periodVideo', params);
};
export const getPerformance = params => {
    return ajax.get('performance', params);
};
export const getTeam = params => {
    return ajax.get('team', params);
};

export const getCourseList = () => {
    return ajax.get('course');
};
export const setNickName = name => {
    return ajax.put('' + name);
};

export const getConfig = keys => {
    return ajax.get('config?keys=' + keys);
};
export const getMessages = time => {
    return ajax.get('message', {params: {time: time}});
};
export const postMessage = params => {
    return ajax.post('message', params);
};
export const postSMS = phoneNumber => {
    return ajax.post('sms/' + phoneNumber);
};
export const loginByCode = params => {
    return ajax.post('userLogin/loginByCode', params);
};
export const getArticle = params => {
    return ajax.get('article', {params: params});
};
export const getArticleById = articleId => {
    return ajax.get('article/' + articleId);
};


export const getBanner = params => {
    return ajax.get('banner/', params);
};
